/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jose.alberto.villaverde
 */
public class StatisticController {
    
    private String _sep = System.getProperty("file.separator");
	private String _path;

    public StatisticController() {
		String jarPath = System.getProperty("java.class.path");
		int lastDot = jarPath.lastIndexOf(";");
		String aux = jarPath;
		if (lastDot != -1) {
			aux = jarPath.substring(0, lastDot);
		}
		int lastSlash = aux.lastIndexOf(_sep);
		_path = aux.substring(0,lastSlash + 1);

    }


	/**
	*
	*
	* Obté l'infinitiu associat a una forma verbal.
	*
	* @author Marc Molins
	* @param int id: id de l'usuari del que es volen recollir les estadístiques.
	* @return Retorna un vector d'enters
	* @throws -
	*/
    public int[] getStatistics(int id) {
        String _field, _nextID;
        int res[] = new int[6];
        for (int i = 0; i < res.length; i++) {
            res[i] = 0;
        }
        FileControllerD _fc = new FileControllerD();
        String number = String.valueOf(id);
        BufferedReader _input = _fc.getFileR(_path + "users" + _sep + number + _sep + "statistics.info");
        try {
            String _userData = _input.readLine();
            while(_userData != null) {
                StringTokenizer line = new StringTokenizer(_userData);
                while(line.hasMoreTokens()) {
                    _field = line.nextToken();
					
                    if(_field.contains("tw:")) {
                        _nextID = _field.substring(3);
						res[0] = Integer.parseInt(_nextID);
					}
					else if(_field.contains("mw:")) {
                        _nextID = _field.substring(3);
						res[1] = Integer.parseInt(_nextID);
					}
					else if(_field.contains("nn:")) {
                        _nextID = _field.substring(3);
						res[2] = Integer.parseInt(_nextID);
					}
					else if(_field.contains("lr:")) {
						_nextID = _field.substring(3);
						res[3] = Integer.parseInt(_nextID);
					}
					else if(_field.contains("lp:")) {
						_nextID = _field.substring(3);
						res[4] = Integer.parseInt(_nextID);
					}
					else if(_field.contains("av:")) {
						_nextID = _field.substring(3);
						res[5] = Integer.parseInt(_nextID);
					}
				}
				_userData = _input.readLine();
      	    }
		}
		catch(Exception e) {

		}
				
		return res;
    }
	
	
	
	public double getStatisticsMeanVerbs(int id) {
        String _field, _nextID;
		double res = 0.0;
        boolean _found = false;
        FileControllerD _fc = new FileControllerD();
        String number = String.valueOf(id);
        BufferedReader _input = _fc.getFileR(_path + "users"+ _sep + number + _sep + "statistics.info");
        try {
            String _userData = _input.readLine();
            while(_userData != null && !_found) {
                StringTokenizer line = new StringTokenizer(_userData);
                while(line.hasMoreTokens()) {
                    _field = line.nextToken();
					
					if(_field.contains("vm:")) {
						_nextID = _field.substring(3);
						res = Double.parseDouble(_nextID);
						_found = true;
					}
				}
				_userData = _input.readLine();
      	    }
		}
		catch(Exception e) {

		}	
		
		return res;
    }
	
	
    public void setStatistics(int id, int tw, int mw, int numnomi, int learntrules, int learntpairs, int addv, double verbsmean) {
        //FileControllerD _fc = new FileControllerD();
        String number = String.valueOf(id);
		File file = new File(_path  + "users"+ _sep + number + _sep + "statistics.info");
		BufferedWriter _output;
		try {
			_output = new BufferedWriter(new FileWriter(file,false));
			_output.write("tw:"+tw);
			_output.newLine();
			_output.write("mw:"+mw);
			_output.newLine();
			_output.write("nn:"+numnomi);
			_output.newLine();
			_output.write("lr:"+learntrules);
			_output.newLine();
			_output.write("lp:"+learntpairs);
			_output.newLine();
			_output.write("av:"+addv);
			_output.newLine();
			_output.write("vm:"+verbsmean);
			_output.newLine();
			_output.close();
		}
		catch (IOException ex) {
			Logger.getLogger(StatisticController.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
